<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\model\Article;
use app\common\model\ArticleCate;
use app\common\model\Help;
use app\common\model\PersonalArticle;
use think\facade\Db;
use think\facade\View;

class AdminPersonal extends AdminInit
{
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $keyword = request()->param("keyword");
        $search_field = request()->param("search_field");
        if($keyword && $search_field){
            if ($search_field == 'id'){
                $where[] = ["{$search_field}",'=',"{$keyword}"];
            }else{
                $where[] = ["{$search_field}",'like',"%{$keyword}%"];
            }
        }
        if(!isset($where)){
            $where = 1;
        }
        
        $list = PersonalArticle::where($where)
            ->order("id DESC")
            ->paginate(["query"=>request()->param()]);
        //echo Help::getlastsql();die;
        View::assign("list",$list);
        View::assign("title","文章列表");
        return  view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        View::assign("title","文章发布");
        View::assign("res",[]);
        View::assign("list",[]);


        return \view();
    }



    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
       
        $user = $this->user;
        if(!$user){
            return redirect((string) url('index/index',['redirect' => 'index/user/index']));
        }
        $mod = new \app\common\model\PersonalArticle();
        $d = input("param.");
        $d["status"] = 1;
        $d["uid"] = $user["id"];
        $d["author"] = $user["nickname"];
        $d["userinfo"] = json_encode($user);
        if(!$d["title"]){
            return  json(["code"=>4000,"msg"=>"title不能为空"]);
        }

        if(isset($d["id"])){
            $res = PersonalArticle::update($d);
        }else{
            $res = $mod->save($d);
        }
        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }
    }

  

    

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $res = PersonalArticle::find($id);
        View::assign("res",$res);
        return view('create');
    }


    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = ArticleCate::where("id",$id)->delete();
        if($res){
            return  json(["code"=>200,"msg"=>"成功"]);
        }else{
            return  json(["code"=>4000,"msg"=>"请稍后再试"]);
        }

    }

    public function read($id)
    {
        $res = PersonalArticle::find($id);
        View::assign("res",$res);
        return view();
    }

}
